package Servlet;

import Bean.Administrator;
import Bean.User;
import Service.AdministratorService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张
 * Date: 2025-03-09
 * Time: 12:13
 */
@WebServlet("/AdminLoginServlet")
public class AdminLoginServlet extends HttpServlet {
    private AdministratorService administratorService = new AdministratorService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf8");
        Administrator administrator = new Administrator();
        administrator.setUsername(req.getParameter("name"));
        administrator.setPassword(req.getParameter("password"));
        Administrator loginAdmin = administratorService.Login(administrator);
        if(loginAdmin == null) {
            req.setAttribute("status", "error");
            req.setAttribute("message", "用户不存在!!");
            req.setAttribute("redirectUrl", "login.jsp");
            req.setAttribute("waitSeconds", 2);
            req.getRequestDispatcher("skip.jsp").forward(req,resp);
        }
        //在session中存储登录用户的userId
        HttpSession session = req.getSession();
        session.setAttribute("LoginAdmin",loginAdmin);
        session.setAttribute("LoginAdminId",loginAdmin.getAdministratorId());
        session.setMaxInactiveInterval(60 * 60);
        req.getRequestDispatcher("FlightsServlet?pageNo=1").forward(req,resp);
    }
}
